﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;
using SCMS.CoreBusinessLogic.NotificationsManager;

namespace SCMS.CoreBusinessLogic.ProcurementPlan
{
    public class ProcurementPlanService : IProcurementPlanService
    {
        #region IProcurementPlanService Members

        public List<Model.ProcurementPlan> GetProcurementPlans(Guid countryProgId)
        {
            using(var context = new SCMSEntities())
            {
                var ppList = context.ProcurementPlans.Where(p => p.ProjectDonor.Project.CountryProgrammeId == countryProgId && p.ProjectDonor.EndDate >= DateTime.Today).OrderByDescending(p => p.PreparedOn).ToList();
                foreach (var pp in ppList)
                {
                    var proj = pp.ProjectDonor.Project;
                    var don = pp.ProjectDonor.Donor;
                    var csf = pp.CountrySubOffice;
                    var person = pp.Staff.Person;
                    var desg = pp.Staff.Designation;
                }
                return ppList;
            }
        }

        public Model.ProcurementPlan GetProcurementPlanById(Guid Id)
        {
            using (var context = new SCMSEntities())
            {
                var pp = context.ProcurementPlans.FirstOrDefault(p => p.Id == Id);
                //dummy load
                var proj = pp.ProjectDonor.Project; ;
                var don = pp.ProjectDonor.Donor;
                var csf = pp.CountrySubOffice;
                var person = pp.Staff.Person;
                var desg = pp.Staff.Designation;
                return pp;
            }
        }

        public Model.ProcurementPlan GetProcurementPlanByProjectId(Guid ProjectDonorId)
        {
            using (var context = new SCMSEntities())
            {
                var pp = context.ProcurementPlans.FirstOrDefault(p => p.ProjectDonorId == ProjectDonorId);
                //dummy load
                var proj = pp.ProjectDonor.Project;
                var don = pp.ProjectDonor.Donor;
                var csf = pp.CountrySubOffice;
                var person = pp.Staff.Person;
                var desg = pp.Staff.Designation;
                return pp;
            }
        }

        public List<Model.ProcurementPlanItem> GetProcurementPlanItems(Guid ppId)
        {
            using (var context = new SCMSEntities())
            {
                var ppItems = context.ProcurementPlanItems.Where(p => p.ProcurementPlanId == ppId).OrderBy(p => p.IsRemoved).ThenBy(p => p.AddedToOR).ThenBy(p => p.RequiredDeliveryDate).ThenBy(p => p.Item.Name).ToList();
                foreach (var ppItem in ppItems)
                {
                    var uom = ppItem.Item.UnitOfMeasure;
                    var curr = ppItem.Currency;
                    var pb = ppItem.ProjectBudget;
                    var loc = ppItem.Location;
                    if (ppItem.OrderRequestItemId.HasValue)
                    {
                        var or = ppItem.OrderRequestItem.OrderRequest;
                    }
                }
                return ppItems;
            }
        }

        public List<Model.ProcurementPlanItem> GetProcurementPlanItemsByProjectId(Guid ProjectDonorId)
        {
            using (var context = new SCMSEntities())
            {
                var ppItems = context.ProcurementPlanItems.Where(p => p.ProcurementPlan.ProjectDonorId == ProjectDonorId).ToList();
                foreach (var ppItem in ppItems)
                {
                    var uom = ppItem.Item.UnitOfMeasure;
                    var curr = ppItem.Currency;
                    var pb = ppItem.ProjectBudget;
                    var loc = ppItem.Location;
                }
                return ppItems;
            }
        }

        public Model.ProcurementPlanItem GetProcurementPlanItemById(Guid Id)
        {
            using (var context = new SCMSEntities())
            {
                var ppItem = context.ProcurementPlanItems.FirstOrDefault(p => p.Id == Id);
                //dummy load
                var uom = ppItem.Item.UnitOfMeasure;
                var curr = ppItem.Currency;
                var pb = ppItem.ProjectBudget;
                var loc = ppItem.Location;
                var pp = ppItem.ProcurementPlan;

                return ppItem;
            }
        }

        public bool SaveProcurementPlan(Model.ProcurementPlan pp)
        {
            using (var context = new SCMSEntities())
            {
                if (pp.Id.Equals(Guid.Empty))
                {
                    var ppEntity = context.ProcurementPlans.FirstOrDefault(p => p.ProjectDonorId == pp.ProjectDonorId);
                    if (ppEntity != null)
                    {
                        ppEntity.PreparedBy = pp.PreparedBy;
                        ppEntity.PreparedOn = pp.PreparedOn;
                        ppEntity.PreparingOfficeId = pp.PreparingOfficeId;
                        //let the object reference point to the existing entity from the db
                        pp.Id = ppEntity.Id;
                    }
                    else
                    {
                        pp.Id = Guid.NewGuid();
                        var pd = context.ProjectDonors.FirstOrDefault(p => p.Id == pp.ProjectDonorId);
                        pp.RefNumber = "PP/DRC/" + pd.Project.CountryProgramme.Country.ShortName + "/" + pd.ProjectNumber;
                        context.ProcurementPlans.AddObject(pp);
                    }
                }
                else
                {
                    context.ProcurementPlans.Attach(pp);
                    context.ObjectStateManager.ChangeObjectState(pp, System.Data.EntityState.Modified);
                }
                return context.SaveChanges() > 0;
            }
        }

        public bool SaveProcurementPlanItem(Model.ProcurementPlanItem ppItem)
        {
            using (var context = new SCMSEntities())
            {
                if (ppItem.Id.Equals(Guid.Empty))
                {
                    ppItem.Id = Guid.NewGuid();
                    context.ProcurementPlanItems.AddObject(ppItem);
                }
                else
                {
                    context.ProcurementPlanItems.Attach(ppItem);
                    context.ObjectStateManager.ChangeObjectState(ppItem, System.Data.EntityState.Modified);
                }
                return context.SaveChanges() > 0;
            }
        }

        public bool DeleteProcurementPlanItem(Guid ppItemId)
        {
            using (var context = new SCMSEntities())
            {
                context.ProcurementPlanItems.DeleteObject(context.ProcurementPlanItems.FirstOrDefault(p => p.Id == ppItemId));
                return context.SaveChanges() > 0;

            }
        }

        public bool DeleteProcumentPlan(Guid ppId)
        {
            using (var context = new SCMSEntities())
            {
                context.ProcurementPlans.DeleteObject(context.ProcurementPlans.FirstOrDefault(p => p.Id == ppId));
                return context.SaveChanges() > 0;
            }
        }

        public List<ProcurementPlanItem> GetPPItemsForOR(Guid ProjectDonorId)
        {
            using (var context = new SCMSEntities())
            {
                var ppItems = context.ProcurementPlanItems.Where(p => p.ProcurementPlan.ProjectDonorId == ProjectDonorId && p.IsApproved == true && p.IsRemoved == false && p.AddedToOR == false).OrderBy(p => p.RequiredDeliveryDate).ThenBy(p => p.Item.Name).ToList();
                foreach (var ppItem in ppItems)
                {
                    var uom = ppItem.Item.UnitOfMeasure;
                    var curr = ppItem.Currency;
                    var pb = ppItem.ProjectBudget;
                    var loc = ppItem.Location;
                    var pp = ppItem.ProcurementPlan;
                    if (ppItem.OrderRequestItemId.HasValue)
                    {
                        var or = ppItem.OrderRequestItem.OrderRequest;
                    }
                }
                return ppItems;
            }
        }

        public List<Model.ProcurementPlan> GetPPForApproval(SystemUser currentUser)
        {
            using (var context = new SCMSEntities())
            {
                List<Model.ProcurementPlan> procurementPlans = new List<Model.ProcurementPlan>();
                var approvers = context.Approvers.Where(a => a.UserId == currentUser.Id && a.ActivityCode == NotificationHelper.ppCode && a.ActionType == NotificationHelper.approvalCode).ToList();
                var delegateApprovers = context.Approvers.Where(a => a.AssistantId == currentUser.Id && a.ActivityCode == NotificationHelper.ppCode && a.ActionType == NotificationHelper.approvalCode).ToList();
                if (approvers != null)
                {
                    foreach (var approver in approvers)
                    {
                        var ppList = context.ProcurementPlans.Where(p => p.ProjectDonorId == approver.ProjectDonorId && p.ProcurementPlanItems.Where(i => i.IsApproved == false).Count() > 0).ToList();
                        foreach (var pp in ppList)
                        {
                            var proj = pp.ProjectDonor.Project;
                            var don = pp.ProjectDonor.Donor;
                            var csf = pp.CountrySubOffice;
                            var person = pp.Staff.Person;
                            var desg = pp.Staff.Designation;

                            procurementPlans.Add(pp);
                        }
                    }
                }
                if (delegateApprovers != null)
                {
                    foreach (var approver in delegateApprovers)
                    {
                        var ppList = context.ProcurementPlans.Where(p => p.ProjectDonorId == approver.ProjectDonorId && p.ProcurementPlanItems.Where(i => i.IsApproved == false).Count() > 0).ToList();
                        foreach (var pp in ppList)
                        {
                            var proj = pp.ProjectDonor.Project;
                            var don = pp.ProjectDonor.Donor;
                            var csf = pp.CountrySubOffice;
                            var person = pp.Staff.Person;
                            var desg = pp.Staff.Designation;

                            procurementPlans.Add(pp);
                        }
                    }
                }
                return procurementPlans;
            }
        }

        public List<Model.ProcurementPlanItem> GetPPItemsForApproval(Guid ppId)
        {
            using (var context = new SCMSEntities())
            {
                var ppItems = context.ProcurementPlanItems.Where(p => p.ProcurementPlanId == ppId && p.IsApproved == false).ToList();
                foreach (var ppItem in ppItems)
                {
                    var uom = ppItem.Item.UnitOfMeasure;
                    var curr = ppItem.Currency;
                    var pb = ppItem.ProjectBudget;
                    var loc = ppItem.Location;
                }
                return ppItems;
            }
        }

        #endregion
    }
}
